package com.gxa.order.service.impl;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.gxa.order.config.UploadConfig;
import com.gxa.order.param.ArtOrderParam;
import com.gxa.order.service.CheckService;
import com.gxa.order.service.UploadService;
import com.gxa.order.service.UserService;
import com.gxa.order.upload.UploadUtil;
import dto.ResultDTO;
import entity.ArtOrder;
import com.gxa.order.dao.ArtOrderDao;
import com.gxa.order.service.ArtOrderService;
import entity.Member;
import exception.SystemException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import util.Response;

import javax.annotation.Resource;
import java.sql.Timestamp;
import java.util.List;

/**
 * (ArtOrder)表服务实现类
 *
 * @author makejava
 * @since 2021-05-11 09:20:24
 */
@Service("artOrderService")
public class ArtOrderServiceImpl implements ArtOrderService {
    @Resource
    private ArtOrderDao artOrderDao;
    @Resource
    private UserService userService;
    @Autowired
    private UploadConfig uploadConfig;
    @Resource
    private CheckService checkService;
    @Resource
    private UploadService uploadService;

    /**
     * 通过ID查询单条数据
     *
     * @param artOrderId 主键
     * @return 实例对象
     */
    @Override
    public ResultDTO queryById(Integer artOrderId) {
        if (artOrderId == null){
            throw new SystemException(1001, "人工单的id不能为空");
        }
        ArtOrder artOrder = artOrderDao.queryById(artOrderId);
        return Response.success(200, "成功！", artOrder);
    }

    /**
     * 查询多条数据
     *
     * @param offset 查询起始位置
     * @param limit  查询条数
     * @return 对象列表
     */
    @Override
    public List<ArtOrder> queryAllByLimit(int offset, int limit) {
        return this.artOrderDao.queryAllByLimit(offset, limit);
    }

    /**
     * 新增数据
     *
     * @param artOrder 实例对象
     * @return 实例对象
     */
    @Override
    public ArtOrder insert(ArtOrder artOrder) {
        this.artOrderDao.insert(artOrder);
        return artOrder;
    }

    /**
     * 修改数据
     *
     * @param artOrder 实例对象
     * @return 实例对象
     */
    @Override
    public ArtOrder update(ArtOrder artOrder) {
        this.artOrderDao.update(artOrder);
        return artOrder;
    }

    /**
     * 通过主键删除数据
     *
     * @param artOrderId 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Integer artOrderId) {
        return this.artOrderDao.deleteById(artOrderId) > 0;
    }

    @Override
    public ResultDTO findUser(Integer memberId) {

        //查询申请人是否存在，根据申请人id
        ResultDTO resApply = userService.findByMemberId(memberId);
        if (resApply.getData()==null) {
            throw new SystemException(1001,"声请人不存在");
        }
        //获取返回的乘机人对象
        ObjectMapper objectMapper = new ObjectMapper();
        Member member = objectMapper.convertValue(resApply.getData(), Member.class);

        return Response.success(200,"查询成功",member);
    }
    public Member getMember(ArtOrderParam artOrderParam){
        Member member = new Member();
        member.setMemberBirth(artOrderParam.getMemberBirth());
        member.setMemberName(artOrderParam.getMemberName());
        member.setMemberSurname(artOrderParam.getMemberSurname());
        member.setMemberPellation(artOrderParam.getMemberPellation());
        member.setMemberSex(artOrderParam.getMemberSex());
        member.setMemberNational(artOrderParam.getMemberNational());
        member.setIdCard(artOrderParam.getIdCard());
        member.setIdCardNum(artOrderParam.getIdCardNum());
        return member;
    }
    public ArtOrder getArtOrder(ArtOrderParam artOrderParam){
        ArtOrder artOrder = new ArtOrder();
        artOrder.setApplyId(artOrderParam.getApplyId());
        artOrder.setArtOrderNeed(artOrderParam.getArtOrderNeed());
        artOrder.setArtOrderExpense(artOrderParam.getArtOrderExpense());
        artOrder.setArtOrderMsg(artOrderParam.getArtOrderMsg());
        return artOrder;
    }

    @Override
    public ResultDTO addArtOrder(MultipartFile multipartFile, ArtOrderParam artOrderParam) {
        ArtOrder artOrder = getArtOrder(artOrderParam);
        Member member1 = getMember(artOrderParam);
        //查询申请人是否存在，根据申请人id
        System.out.println("____________________________");
        System.out.println(artOrder);
        ResultDTO resApply = userService.findByMemberId(artOrder.getApplyId());
        if (resApply.getData()==null) {
            throw new SystemException(1001,"声请人不存在");
        }
        //根据乘机人信息，查询是否存在
        ResultDTO resMember = userService.queryMember(member1);
        if (resMember.getData()==null) {
            throw new SystemException(1002,"乘机人不存在");
        }
        //获取返回的乘机人对象
        ObjectMapper objectMapper = new ObjectMapper();
        Member member = objectMapper.convertValue(resMember.getData(), Member.class);
        artOrder.setMemberId(member.getMemberId());
        artOrder.setCreateTime(new Timestamp(System.currentTimeMillis()));
        int i = artOrderDao.add(artOrder);
        if (i<=0){
            throw new SystemException(1003,"增加订单失败");
        }

        if (multipartFile != null){
            String fileName = multipartFile.getOriginalFilename();
            System.out.println("------------------------------------------");
            System.out.println(fileName);
//            String path = uploadConfig.getPath();
//            String exts = uploadConfig.getExts();
//            String uploadFile = UploadUtil.uploadFile(multipartFile, exts, path);
            ResultDTO upload = uploadService.upload(multipartFile);
            if (upload.getData()==null){
                throw new SystemException(1004,"图片上传失败");
            }
            String s = objectMapper.convertValue(upload.getData(), String.class);
            int i1 = artOrderDao.updateOrder(artOrder.getArtOrderId(), s);
            if (i1 <= 0 ){
                throw new SystemException(1005,"图片上传失败");
            }
        }

        //生成审批单
//        ResultDTO resArt = checkService.addArt(artOrder.getArtOrderId());
//        if (resArt.getData()==null) {
//            throw new SystemException(1003,"生成审批单失败");
//        }
        return Response.success(200,"订单生成成功");
    }
    /**
     * 获取订单编号
     * @param applyId
     * @return
     */
    public String getOrderCode(Integer applyId){
        return "编号"+applyId+System.currentTimeMillis();
    }
}
